********************************************************************************
/*ANALYSIS 
- Exporting matrices to for_R file for plotting         
*/
********************************************************************************

clear
	clear matrix
	clear mata
	set more off
	set mem 5000m
	set maxvar 20000

cd "/Users/georgemelios/Downloads/PSRM_Replication 3"
use "DATA/final_cjoint.dta", clear
 
global cj = "cj_matchpid cj_affect cj_ideology cj_race cj_edu cj_diet cj_beauty cj_height"
global cj_match_short = "cj_matchpid cj_matchaff cj_matchid cj_matchrace  cj_matched cj_matchdiet cj_matchbeau cj_matchhei"
global cj_match_long = "cj_matchpid2 cj_matchaff2 cj_matchid2 cj_matchrace2 cj_matched2 cj_matchdiet2 cj_matchbeau2 cj_matchhei2 "

**# Table A2: Main conjoint analysis results
conjoint choice $cj , est(amce) id(caseid) 
matrix m1 = e(results)
mat2txt , matrix(m1) saving("DATA/for_R/main_results.txt") replace
	//save N
matrix observations = e(N) , e(N_clust)

**# Table A4: Conjoint analysis results using matched attributes
conjoint choice $cj_match_short , est(amce) id(caseid) 
matrix m1 = e(results)
mat2txt , matrix(m1) saving("DATA/for_R/match_results_short.txt") replace
	//save N
matrix observations = observations \ e(N), e(N_clust)

**# Table A5: Conjoint analysis results using matched attributes (disaggregated)
conjoint choice $cj_match_long , est(amce) id(caseid) 
matrix m1 = e(results)
mat2txt , matrix(m1) saving("DATA/for_R/match_results_long.txt") replace
	//save N
matrix observations = observations \ e(N), e(N_clust)

**# Table A6: Preference for matched tolerance by partisanship: (Uses matched affect coded based on partisanship in/out)
conjoint choice cj_matchaff3 cj_matchid2 cj_matchrace2 cj_matched2 cj_matchdiet2 cj_matchbeau2 cj_matchhei2 , est(amce) id(caseid) 
matrix m1 = e(results)
mat2txt , matrix(m1) saving("DATA/for_R/match_tolerance_party.txt") replace
	//save N
matrix observations = observations \ e(N), e(N_clust)

**# Table A7: Conjoint analysis results by gender
conjoint choice $cj, subgroup(sex) est(amce) id(caseid) 
matrix male = e(results_Men)
matrix female = e(results_Women)
	//export to R for plotting
mat2txt , matrix(male) saving("DATA/for_R/male_results.txt") replace
mat2txt , matrix(female) saving("DATA/for_R/female_results.txt") replace
	//save N
matrix observations = observations \ e(N_Men), e(N_clust_Men)
matrix observations = observations \ e(N_Women), e(N_clust_Women)

**# Table A8: Conjoint analysis results by gender with relative education and height
conjoint choice cj_matchpid cj_affect cj_ideology cj_race more_educated cj_diet cj_beauty shorter_taller, subgroup(sex) est(amce) id(caseid)  
matrix male_match = e(results_Men)
matrix female_match = e(results_Women)
	//export to R for plotting
mat2txt , matrix(male_match) saving("DATA/for_R/male_match.txt") replace
mat2txt , matrix(female_match) saving("DATA/for_R/female_match.txt") replace
	//save N
matrix observations = observations \ e(N_Men), e(N_clust_Men)
matrix observations = observations \ e(N_Women), e(N_clust_Women)

**# Table A9: Conjoint analysis results by gender and party
//Labour panel
conjoint choice $cj if party_id == 1, subgroup(sex) est(amce) id(caseid)  
matrix male_labour = e(results_Men)
matrix female_labour = e(results_Women)
	//export to R for plotting
mat2txt , matrix(male_labour) saving("DATA/for_R/male_labour.txt") replace
mat2txt , matrix(female_labour) saving("DATA/for_R/female_labour.txt") replace
	//save N
matrix observations = observations \ e(N_Men), e(N_clust_Men)
matrix observations = observations \ e(N_Women), e(N_clust_Women)

//Tory panel
conjoint choice $cj if party_id == 2, subgroup(sex) est(amce) id(caseid) 
matrix male_tory = e(results_Men)
matrix female_tory = e(results_Women)
	//export to R for plotting
mat2txt , matrix(male_tory) saving("DATA/for_R/male_tory.txt") replace
mat2txt , matrix(female_tory) saving("DATA/for_R/female_tory.txt") replace
	//save N
matrix observations = observations \ e(N_Men), e(N_clust_Men)
matrix observations = observations \ e(N_Women), e(N_clust_Women)

**# Table A10: Conjoint analysis results by partisanship
gen flag = .
replace flag = 1 if party_id == 1 & cj_party == 0 //labour rating tory
replace flag = 2 if party_id == 2 & cj_party == 1 //tory rating labour

conjoint choice cj_affect cj_race cj_diet cj_ideology cj_edu cj_beauty cj_height, est(amce) id(caseid) subgroup(flag) 
matrix labour = e(results__1)
mat2txt , matrix(labour) saving("DATA/for_R/labour_stereo.txt") replace
matrix tory = e(results__2)
mat2txt , matrix(tory) saving("DATA/for_R/tory_stereo.txt") replace

matrix observations = observations \ e(N__1), e(N_clust__1)
matrix observations = observations \ e(N__2), e(N_clust__2)

mat2txt , matrix(observations) saving("DATA/for_R/observations.txt") replace

*Figure 5: Interaction of political tolerance and partisanship in date selection
reg choice i.cj_affect##i.cj_matchpid cj_race cj_diet cj_ideology cj_edu cj_beauty cj_height, vce(cluster caseid) 
margins cj_affect, at(cj_matchpid=(0 1)) post
matrix tolerance = e(b)
mat2txt , matrix(tolerance) saving("DATA/for_R/tolerance.txt") replace
					
*Figure 6: Interaction of non-political attributes and partisanship in date selection
logit choice cj_affect cj_race cj_diet cj_ideology cj_edu i.cj_beauty##i.cj_matchpid cj_height, vce(cluster caseid)
margins cj_beauty, at(cj_matchpid=(0 1)) post //0.3 0.7
matrix beauty = e(b)
mat2txt , matrix(beauty) saving("DATA/for_R/beauty.txt") replace

logit choice cj_affect cj_race cj_diet cj_ideology cj_edu cj_beauty i.cj_height##i.cj_matchpid, vce(cluster caseid)
margins cj_height, at(cj_matchpid=(0 1)) post
matrix height = e(b)
mat2txt , matrix(height) saving("DATA/for_R/height.txt") replace

*Figure A1: Density of preferences by attribute per number of choice sets

local attrs "cj_affect cj_edu cj_beauty cj_height cj_diet cj_ideology cj_race cj_matchpid"

foreach att in `attrs' {
    * flag where the chosen alternative was "cj_attr = 1"
    gen chose_`att' = (choice == 1 & `att' == 1)
    * Sum these flags for each respondent
    egen sum_chose_`att' = sum(chose_`att'), by(caseid)
}

replace sum_chose_cj_matchpid = . if sum_chose_cj_matchpid ==0 //missing

twoway (hist sum_chose_cj_beauty, discrete recast(line) lc(red) legend(label(1 "Attractive"))) ///
       (hist sum_chose_cj_edu, discrete recast(line) lc(blue) legend(label(2 "Degree"))) ///
       (hist sum_chose_cj_affect, discrete recast(line) lc(green) legend(label(3 "Affect"))) ///
       (hist sum_chose_cj_height, discrete recast(line) lc(purple) legend(label(4 "Height"))) (hist sum_chose_cj_diet, discrete recast(line) lc(cyan) legend(label(5 "Diet"))) ///
       (hist sum_chose_cj_ideology, discrete recast(line) lc(orange) legend(label(6 "Ideology"))) ///
       (hist sum_chose_cj_race, discrete recast(line) lc(yellow) legend(label(7 "Race"))) ///
       (hist sum_chose_cj_matchpid, discrete recast(line) lc(magenta) legend(label(8 "MatchPID")))

graph export "FIG/Figure_A1.png", replace	  





 
